package com.hkrt.demo.presentation.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author hkrt 常用的工具类
 */
public class CommonUtils {
	private static boolean checkNetState = true;
	private static Context Myactivity;
	private static final int BLACK = 0xff000000;
	public static String getTime(String format) {
		DateFormat df = new SimpleDateFormat(format);
		String time = df.format(new Date());
		return time;
	}
	
	

	/**
	   * 获取现在时间
	   * 
	   * @return返回短时间格式 yyyy-MM-dd
	   */
	public static Date getDateFromTime(String time){
		
		
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");         
//		DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");         
		Date date = null;    
		            
		// String转Date    
//		str = "2007-1-18";          
		try {    
		           date = format1.parse(time);   
//		           data = format2.parse(str); 
		} catch (ParseException e) {    
		           e.printStackTrace();    
		} 
		
		return date;
		
	};

	/**
	 * 
	 * @param d
	 *            今天
	 * @param day
	 *            几天前
	 * @return
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	public static String getTime(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		String time = df.format(date);
		return time;
	}

	public static String getYearTime() {
		DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
		String time = df.format(new Date());
		return time;
	}

	public static String getMonthTime() {
		DateFormat df = new SimpleDateFormat("MMddHHmmss");
		String time = df.format(new Date());
		return time;
	}

	public static String getFormatTime(String date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String time = df.format(new Date(date));
		return time;
	}

	public static String getFormatTime_(String date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		DateFormat df_ = new SimpleDateFormat(format);
		Date date_ = null;
		try {
			date_ = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String time = df_.format(date_);
		return time;
	}

	public static boolean isTaday(String time) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		boolean isTaday = false;
		try {
			date = format.parse(time);
			long dateTime = date.getTime();
			isTaday = DateUtils.isToday(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return isTaday;

	}

	/**
	 * 
	 * @param time
	 *            如：20130809121308
	 * @param timeFormat
	 *            日期格式，如：yyyyMMddHHmmss,yyyy-MM-dd HH:mm:ss
	 * @return
	 */

	public static long getTimeLong(String time, String timeFormat) {

		SimpleDateFormat format = new SimpleDateFormat(timeFormat);
		Date date = null;
		long dayTime = 0;
		try {
			date = format.parse(time);
			dayTime = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return dayTime;

	}


	public static String replaceSubString(String str, int n) {
		String sub = "";
		try {
			if (str == "") {
				return "";
			}
			if (str == null) {
				return "";
			}
			if (str.length() < 16) {
				return str;
			}
			String strs = str.substring(13);
			sub = str.substring(0, str.length() - n);
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < 4; i++) {
				sb = sb.append("*");
			}
			sub += sb.toString();
			sub += strs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sub;
	}

	public static String replaceSubStringNew(String str) {

		String sub = "";
		try {
			if (str == "") {
				return "";
			}
			if (str == null) {
				return "";
			}
			if (str.length() < 16) {
				return str;
			}
			sub = str.substring(0, 6) + "******"
					+ str.substring(str.length() - 4);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sub;
	}

	public static String toHexString(String s) {

		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	public static String byteToString(byte b) {
		byte high, low;
		byte maskHigh = (byte) 0xf0;
		byte maskLow = 0x0f;
		high = (byte) ((b & maskHigh) >> 4);
		low = (byte) (b & maskLow);
		StringBuffer buf = new StringBuffer();
		buf.append(findHex(high));
		buf.append(findHex(low));
		return buf.toString();
	}

	private static char findHex(byte b) {
		int t = new Byte(b).intValue();
		t = t < 0 ? t + 16 : t;
		if ((0 <= t) && (t <= 9)) {
			return (char) (t + '0');
		}
		return (char) (t - 10 + 'A');
	}

	public static String moneyToZeroFormat(String money) {

		String money_ = null;
		if (money.contains(".")) {

			String[] moneyStr = money.split("\\.");
			String centMoney = moneyStr[1];
			if (centMoney.length() < 2) {
				centMoney = centMoney + "0";
			}
			money_ = moneyStr[0] + centMoney;
		} else {
			money_ = money + "00";
		}

		int len = money_.length();

		int left = 12 - len;
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < left; i++) {
			str.append("0");
		}

		String s = str + money_;
		return s;
	}

	/**
	 * 16进制格式的字符串转成16进制byte 44 --> byte 0x44
	 * 
	 * @param hexString
	 * @return
	 */
	public static byte[] HexStringToByteArray(String hexString) {//
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		if (hexString.length() == 1 || hexString.length() % 2 != 0) {
			hexString = "0" + hexString;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	/**
	 * 对double数据进行取精度.
	 * <p>
	 * For example: <br>
	 * double value = 100.345678; <br>
	 * double ret = round(value,4,BigDecimal.ROUND_HALF_UP); <br>
	 * ret为100.3457 <br>
	 * 
	 * @param value
	 *            double数据.
	 * @param scale
	 *            精度位数(保留的小数位数).
	 * @param roundingMode
	 *            精度取值方式.
	 * @return 精度计算后的数据.
	 */
	public static double round(double value, int scale, int roundingMode) {
		BigDecimal bd = new BigDecimal(value);
		bd = bd.setScale(scale, roundingMode);
		double d = bd.doubleValue();
		bd = null;
		return d;
	}

	/**
	 * 返回当前程序版本名
	 */
	public static String getAppVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionName = pi.versionName;
			if (versionName == null || versionName.length() <= 0) {
				return "";
			}
		} catch (Exception e) {
		}
		return versionName;
	}

	/**
	 * 保留小数点后2位
	 * 
	 * @param s
	 * @return
	 */
	@SuppressLint("NewApi")
	public static String format(String s) {
		if (s == null || s.length() < 1) {
			return "";
		}
		NumberFormat formater = null;
		double num = Double.parseDouble(s);
		formater = new DecimalFormat("#,##0.00");

		formater.setRoundingMode(RoundingMode.DOWN);
		String moneyVal = formater.format(num);

		return moneyVal;

	}

	/**
	 * 保留小数后1位
	 * 
	 * @param
	 * @return
	 */
	public static String formatNum(String str) {
		if (str == null || str.length() < 1) {
			return "";
		}
		NumberFormat formater = null;
		double num = Double.parseDouble(str);
		formater = new DecimalFormat("#,##0.0");

		String moneyVal = formater.format(num);
		return moneyVal;

	}
	
	
	/**
	 * 保留小数后0位
	 * 
	 * @param
	 * @return
	 */
	@SuppressLint("NewApi")
	public static String formatNum_(String str) {
		if (str == null || str.length() < 1) {
			return "";
		}
		NumberFormat formater = null;
		double num = Double.parseDouble(str);
//		formater = new DecimalFormat("#,##0");
		formater = new DecimalFormat("#.##");

		formater.setRoundingMode(RoundingMode.DOWN);
		String moneyVal = formater.format(num);
		
		return moneyVal;

	}
	
	
	/**
	 * 保留小数后3位
	 * 
	 * @param
	 * @return
	 */
	@SuppressLint("NewApi")
	public static String formatNumThree(String str) {
		if (str == null || str.length() < 1) {
			return "";
		}
		NumberFormat formater = null;
		double num = Double.parseDouble(str);
		formater = new DecimalFormat("#,##0.0");

		formater.setRoundingMode(RoundingMode.DOWN);
		String moneyVal = formater.format(num);
		
		return moneyVal;

	}

	public static boolean containStr(String str) {

		int count = 0;
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		while (m.find()) {
			for (int i = 0; i <= m.groupCount(); i++) {
				count = count + 1;
			}
		}
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}



	private void setIsClose(AlertDialog dialog, boolean isClose) {

		try {

			Field field = dialog.getClass().getSuperclass()
					.getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(dialog, false);

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	/** toast */
	public static void mMakeTextToast(Context context, String text,
			boolean isLong) {

		if (isLong == true) {
			Toast.makeText(context, text, Toast.LENGTH_LONG).show();

		} else {
			Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
		}
	}

	/** toast */
	public static void mMakeResIdToast(Context context, int resId,
			boolean isLong) {

		if (isLong == true) {
			Toast.makeText(context, resId, Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 
	 * @param s
	 * @return
	 */
	public static String formatTo12Zero(String s) {
		if (s == null || s.length() < 1 || s.length() > 12) {
			return "";
		}
		int len = s.length();
		int len_ = 12 - len;
		StringBuffer moneyNum = new StringBuffer(s);
		for (int i = 0; i < len_; i++) {

			moneyNum.append("0");

		}

		return moneyNum.toString();

	}

	// 把yyyymmdd转成yyyy-MM-dd格式

	public static String formatDate(String str) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sfstr;
	}
	
	// 把yyyymmdd转成指定格式例如yyyy-MM-dd

	public static String getDateFromFormat(String str,String format) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sf2 = new SimpleDateFormat(format);
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sfstr;
	}
	
	public static String getDateFromFormat_(String str,String oldFormat,String newFormat) {
		SimpleDateFormat sf1 = new SimpleDateFormat(oldFormat);
		SimpleDateFormat sf2 = new SimpleDateFormat(newFormat);
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sfstr;
	}

	public static void bankCardNumAddSpace(final TextView creditCardNum) {
		
		creditCardNum.addTextChangedListener(new TextWatcher() {
			int beforeTextLength = 0;
			int onTextLength = 0;
			boolean isChanged = false;

			int location = 0;// 记录光标的位置
			private char[] tempChar;
			private StringBuffer buffer = new StringBuffer();
			int konggeNumberB = 0;

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				beforeTextLength = s.length();
				if (buffer.length() > 0) {
					buffer.delete(0, buffer.length());
				}
				konggeNumberB = 0;
				for (int i = 0; i < s.length(); i++) {
					if (s.charAt(i) == ' ') {
						konggeNumberB++;
					}
				}
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				onTextLength = s.length();
				buffer.append(s.toString());
				if (onTextLength == beforeTextLength || onTextLength <= 3
						|| isChanged) {
					isChanged = false;
					return;
				}
				isChanged = true;
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (isChanged) {
					location = creditCardNum.getSelectionEnd();
					int index = 0;
					while (index < buffer.length()) {
						if (buffer.charAt(index) == ' ') {
							buffer.deleteCharAt(index);
						} else {
							index++;
						}
					}

					index = 0;
					int konggeNumberC = 0;
					while (index < buffer.length()) {
						if ((index == 4 || index == 9 || index == 14 || index == 19)) {
							buffer.insert(index, ' ');
							konggeNumberC++;
						}
						index++;
					}

					if (konggeNumberC > konggeNumberB) {
						location += (konggeNumberC - konggeNumberB);
					}

					tempChar = new char[buffer.length()];
					buffer.getChars(0, buffer.length(), tempChar, 0);
					String str = buffer.toString();
					if (location > str.length()) {
						location = str.length();
					} else if (location < 0) {
						location = 0;
					}

					creditCardNum.setText(str);
					Editable etable = (Editable) creditCardNum.getText();
					Selection.setSelection(etable, location);
					isChanged = false;
				}
			}
		});
	}
	
	
	public static void phoneNumAddSpace(final TextView creditCardNum) {
		
		creditCardNum.addTextChangedListener(new TextWatcher() {
			int beforeTextLength = 0;
			int onTextLength = 0;
			boolean isChanged = false;

			int location = 0;// 记录光标的位置
			private char[] tempChar;
			private StringBuffer buffer = new StringBuffer();
			int konggeNumberB = 0;

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				beforeTextLength = s.length();
				if (buffer.length() > 0) {
					buffer.delete(0, buffer.length());
				}
				konggeNumberB = 0;
				for (int i = 0; i < s.length(); i++) {
					if (s.charAt(i) == ' ') {
						konggeNumberB++;
					}
				}
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				onTextLength = s.length();
				buffer.append(s.toString());
				if (onTextLength == beforeTextLength || onTextLength <= 3
						|| isChanged) {
					isChanged = false;
					return;
				}
				isChanged = true;
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (isChanged) {
					location = creditCardNum.getSelectionEnd();
					int index = 0;
					while (index < buffer.length()) {
						if (buffer.charAt(index) == ' ') {
							buffer.deleteCharAt(index);
						} else {
							index++;
						}
					}

					index = 0;
					int konggeNumberC = 0;
					while (index < buffer.length()) {
						if ((index == 3 || index == 8)) {
							buffer.insert(index, ' ');
							konggeNumberC++;
						}
						index++;
					}

					if (konggeNumberC > konggeNumberB) {
						location += (konggeNumberC - konggeNumberB);
					}

					tempChar = new char[buffer.length()];
					buffer.getChars(0, buffer.length(), tempChar, 0);
					String str = buffer.toString();
					if (location > str.length()) {
						location = str.length();
					} else if (location < 0) {
						location = 0;
					}

					creditCardNum.setText(str);
					Editable etable = (Editable) creditCardNum.getText();
					Selection.setSelection(etable, location);
					isChanged = false;
				}
			}
		});
	}
	
	public static Double div(Double v1,Double v2,int scale){

        if(scale<0){

            throw new IllegalArgumentException(

            "The scale must be a positive integer or zero");

        }

        BigDecimal b1 = new BigDecimal(v1.toString());

        BigDecimal b2 = new BigDecimal(v2.toString());

        return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

    }
	

    public static String getTime() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = df.format(new Date());
        return time;
    }
	
	/** 
     * 把中文转成Unicode码 
     * @param str 
     * @return 
     */  
    public static String chinaToUnicode(String str){  
        String result="";  
        for (int i = 0; i < str.length(); i++){  
            int chr1 = (char) str.charAt(i);  
            if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)  
                result+="\\u" + Integer.toHexString(chr1);  
            }else{  
                result+=str.charAt(i);  
            }  
        }  
        return result;  
    }
    
    /** 
     * 判断是否是全是中文 
     * @param str 
     * @return 
     */  
    public static boolean isChina(String str){  
        String result="";  
        int isChinese = 0;
        int noChinese = 0;
        for (int i = 0; i < str.length(); i++){  
            int chr1 = (char) str.charAt(i);  
            if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)  
                isChinese++;
            }else{  
                noChinese++;  
            }  
        }  
        if(noChinese>0){
        	return false;
        }else{
        	return true;
        }
    }
	
	/**
	 * 必传图片的数目
	 * 
	 * @param path
	 * @return
	 */
	public static int mustUploadPhotosSize(String path) {
		File file = new File(path);
		String[] list = file.list();
		int i = 0;
		if (list == null) {
			return i;
		}
		for (String string : list) {
			if (string.contains("@@")) {
				i++;
			}
		}
		return i;

	}
	
	/**
	 * 必传图片的路径的集合
	 * 
	 * @param path
	 * @return
	 */
	public static ArrayList<String> mustUploadPhotosPaths(String path) {
		File file = new File(path);
		String[] list = file.list();
		ArrayList<String> arrayList = new ArrayList<String>();
		if (list == null) {
			return arrayList;
		}
		for (String string : list) {
			if (string.contains("@@")) {
				arrayList.add(path+string);
			}
		}
		return arrayList;

	}
	
	/**
	 * 可传图片的路径的集合
	 * 
	 * @param path
	 * @return
	 */
	public static ArrayList<String> canUploadPhotosPaths(String path) {
		File file = new File(path);
		String[] list = file.list();
		ArrayList<String> arrayList = new ArrayList<String>();
		if (list == null) {
			return arrayList;
		}
		for (String string : list) {
			if (string.contains("_canUpload")) {
				arrayList.add(path+string);
			}
		}
		return arrayList;
	}
	
	/**
	 * TODO 缩放图片
	 * 
	 * @param imagePath
	 * @param scale
	 *            压缩比率 可以为空,那么是默认是200,越大压缩比越小
	 * @return 一个缩放好的bitmap
	 * 此压缩方法存在的弊端是：不能精确压缩图片的大小，只能整数倍压缩，因为压缩参数为int型
	 */
	public static Bitmap getZoomBitmap(String imagePath, Float scale) {
		if (!new File(imagePath).exists())
			return null;
		if (scale == null) {
			scale = 450f;
		}
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		Bitmap bm = BitmapFactory.decodeFile(imagePath, options);
		int beH = (int) ((options.outHeight / (float) scale)+0.5);
		int beW = (int) ((options.outWidth / (float) scale)+0.5);
		int be = 1;
		be = (beH >= beW) ? beH : beW;
		if (be <= 1) {
			be = 1;
		}
		options.inSampleSize = be;// be=2.表示压缩为原来的1/2,以此类推
		options.inJustDecodeBounds = false;
		bm = BitmapFactory.decodeFile(imagePath, options);
		return bm;
	}
	
	/**
	 * 删除指定文件 param path 文件完整绝对路径
	 * */
	public static boolean delFile(String path) {
		File file = new File(path);
		if (file.isFile()) {
			return file.delete();
		}
		return false;
	}
	
	//自动弹出键盘
	public static void showEditViewSoftInput(final EditText editText) {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				InputMethodManager inputManager = (InputMethodManager) editText
						.getContext().getSystemService(
								Context.INPUT_METHOD_SERVICE);
				inputManager.showSoftInput(editText, 0);
			}
		}, 998);
	}
}
